package com.ruoyi.pims.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pims.domain.SalesRecord;
import com.ruoyi.pims.service.ISalesRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 销售执行记录Controller
 * 
 * @author ruoyi
 * @date 2025-06-17
 */
@Controller
@RequestMapping("/pims/record")
public class SalesRecordController extends BaseController
{
    private String prefix = "pims/record";

    @Autowired
    private ISalesRecordService salesRecordService;

    @RequiresPermissions("pims:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询销售执行记录列表
     */
    @RequiresPermissions("pims:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SalesRecord salesRecord)
    {
        startPage();
        List<SalesRecord> list = salesRecordService.selectSalesRecordList(salesRecord);
        return getDataTable(list);
    }

    /**
     * 导出销售执行记录列表
     */
    @RequiresPermissions("pims:record:export")
    @Log(title = "销售执行记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SalesRecord salesRecord)
    {
        List<SalesRecord> list = salesRecordService.selectSalesRecordList(salesRecord);
        ExcelUtil<SalesRecord> util = new ExcelUtil<SalesRecord>(SalesRecord.class);
        return util.exportExcel(list, "销售执行记录数据");
    }

    /**
     * 新增销售执行记录
     */
    @RequiresPermissions("pims:record:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存销售执行记录
     */
    @RequiresPermissions("pims:record:add")
    @Log(title = "销售执行记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SalesRecord salesRecord)
    {
        return toAjax(salesRecordService.insertSalesRecord(salesRecord));
    }

    /**
     * 修改销售执行记录
     */
    @RequiresPermissions("pims:record:edit")
    @GetMapping("/edit/{executionId}")
    public String edit(@PathVariable("executionId") Long executionId, ModelMap mmap)
    {
        SalesRecord salesRecord = salesRecordService.selectSalesRecordByExecutionId(executionId);
        mmap.put("salesRecord", salesRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存销售执行记录
     */
    @RequiresPermissions("pims:record:edit")
    @Log(title = "销售执行记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SalesRecord salesRecord)
    {
        return toAjax(salesRecordService.updateSalesRecord(salesRecord));
    }

    /**
     * 删除销售执行记录
     */
    @RequiresPermissions("pims:record:remove")
    @Log(title = "销售执行记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(salesRecordService.deleteSalesRecordByExecutionIds(ids));
    }
}
